package com.lisx.module_matters.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.fenghuajueli.lib_data.constants.EventBusConstants;
import com.fenghuajueli.lib_data.entity.db.MattersDbEntity;
import com.fenghuajueli.lib_data.entity.eventbus.EventEntity;
import com.fenghuajueli.libbasecoreui.dialogfragment.MessageDialogFragment;
import com.fenghuajueli.libbasecoreui.manager.MattersManager;
import com.fenghuajueli.libbasecoreui.mvp.OtherBaseFragment;
import com.lisx.module_matters.R;
import com.lisx.module_matters.adapter.MattersItemAdapter;
import com.lisx.module_matters.databinding.FragmentMattersItemBinding;
import com.lisx.module_matters.dialogfragment.AddMattersNewDialogFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MattersItemFragment extends OtherBaseFragment {

    private int index;
    private String tabTitle;
    private MattersItemAdapter adapter;
    private FragmentMattersItemBinding binding;

    @Override
    public void initView(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        binding = FragmentMattersItemBinding.bind(getContentView());
        Bundle arguments = getArguments();
        if (arguments != null) {
            index = arguments.getInt("index", 0);
            tabTitle = arguments.getString("tab_title");
        }
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new MattersItemAdapter(tabTitle);
        binding.recyclerview.setAdapter(adapter);

        adapter.setOnItemClickListener(new MattersItemAdapter.OnItemClickListener() {
            @Override
            public void onUpdate(MattersDbEntity mattersDbEntity, int update) {
                updateMatters(mattersDbEntity, update);
            }

            @Override
            public void onDelete(MattersDbEntity mattersDbEntity) {
                MessageDialogFragment fragment = new MessageDialogFragment();
                fragment.setMessageContent("确认删除此事项?");
                fragment.setMessageDialogListener(new MessageDialogFragment.MessageDialogListener() {
                    @Override
                    public void onRightClick() {
                        deleteMatters(mattersDbEntity);
                    }

                    @Override
                    public void onLeftClick() {

                    }
                });
                fragment.show(getChildFragmentManager());
            }

            @Override
            public void onEdit(MattersDbEntity mattersDbEntity) {
                AddMattersNewDialogFragment fragment = new AddMattersNewDialogFragment();
                fragment.setData(mattersDbEntity);
                fragment.show(getChildFragmentManager());
            }
        });

        setData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    private void deleteMatters(MattersDbEntity mattersDbEntity) {
        Observable.create(new ObservableOnSubscribe<List<MattersDbEntity>>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<List<MattersDbEntity>> emitter) throws Exception {
                List<MattersDbEntity> mattersData = MattersManager.getInstance().deleteMattersDbEntity(mattersDbEntity);
                emitter.onNext(mattersData);
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<List<MattersDbEntity>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull List<MattersDbEntity> mattersDbEntities) {
                        if (mattersDbEntities != null && mattersDbEntities.size() > 0) {
                            adapter.setData(mattersDbEntities);
                            binding.recyclerview.setVisibility(View.VISIBLE);
                            binding.noDataContainer.setVisibility(View.GONE);
                        } else {
                            binding.recyclerview.setVisibility(View.GONE);
                            binding.noDataContainer.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void updateMatters(MattersDbEntity mattersDbEntity, int update) {
        Observable.create(new ObservableOnSubscribe<List<MattersDbEntity>>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<List<MattersDbEntity>> emitter) throws Exception {
                List<MattersDbEntity> mattersData = MattersManager.getInstance().updateMattersDbEntity(mattersDbEntity, update);
                emitter.onNext(mattersData);
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<List<MattersDbEntity>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull List<MattersDbEntity> mattersDbEntities) {
                        if (mattersDbEntities != null && mattersDbEntities.size() > 0) {
                            adapter.setData(mattersDbEntities);
                            binding.recyclerview.setVisibility(View.VISIBLE);
                            binding.noDataContainer.setVisibility(View.GONE);
                        } else {
                            binding.recyclerview.setVisibility(View.GONE);
                            binding.noDataContainer.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void setData() {
        Observable.create(new ObservableOnSubscribe<List<MattersDbEntity>>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<List<MattersDbEntity>> emitter) throws Exception {
                List<MattersDbEntity> mattersData = MattersManager.getInstance().getMattersData(index);
                emitter.onNext(mattersData);
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<List<MattersDbEntity>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull List<MattersDbEntity> mattersDbEntities) {
                        if (mattersDbEntities != null && mattersDbEntities.size() > 0) {
                            adapter.setData(mattersDbEntities);
                            binding.recyclerview.setVisibility(View.VISIBLE);
                            binding.noDataContainer.setVisibility(View.GONE);
                        } else {
                            binding.recyclerview.setVisibility(View.GONE);
                            binding.noDataContainer.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_matters_item;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void event(EventEntity eventBusEntity) {
        if (eventBusEntity.getCode() == EventBusConstants.UPDATE_MATTERS) {
            setData();
        }

    }
}
